package com.coinworld.producer;

import com.google.gson.Gson;
import com.coinworld.entity.User;
import com.coinworld.entity.vo.RabbitmqDelayVo;
import com.coinworld.entity.vo.RabbitmqVo;
import com.coinworld.log.MqSendLog;
import com.coinworld.utils.ShareCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * Created by ZhangRuiFeng on 2019/5/15.
 */

@Service
public class SendService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RabbitTemplate template;

    private Gson gson = new Gson();

    public String sendDelayMsg(RabbitmqDelayVo vo) throws Exception {
        Message message =buildMessage(vo.getId().toString());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        message.getMessageProperties().setExpiration(vo.getValid().toString());
        template.convertAndSend("enqueQueue", message);
        System.out.println("发送消息:"+new Date().toString()+" 内容："+new String(message.getBody(),"UTF-8"));
        return "OK";
    }

    private Message buildMessage(String data) {
        Message message = MessageBuilder.withBody(data.getBytes())
                .setMessageId(UUID.randomUUID().toString()).setContentType("application/json").build();
        return message;
    }

    public void sendRewardMsg(RabbitmqVo rabbitmqVo)
    {
        template.convertAndSend("rewardQueue",rabbitmqVo);
    }

    public void sendRewardMsg()
    {
        RabbitmqVo rabbitmqVo=new RabbitmqVo();
        rabbitmqVo.setType(5);
        rabbitmqVo.setMobile("15901249589");
        rabbitmqVo.setTrxId(UUID.randomUUID().toString());
//        rabbitmqVo.setMoney(new BigDecimal(100));
//        rabbitmqVo.setOrderNo(orderNo);
//        rabbitmqVo.setPayType(payType);
        MqSendLog.logData(new Gson().toJson(rabbitmqVo));
        template.convertAndSend("rewardQueue",rabbitmqVo);
    }



    public void setRabbitMqVo(String mobile,Integer type,String orderNo,BigDecimal money,String payType){
        RabbitmqVo rabbitmqVo=new RabbitmqVo();
        rabbitmqVo.setType(type);
        rabbitmqVo.setMobile(mobile);
        rabbitmqVo.setMoney(money);
        rabbitmqVo.setOrderNo(orderNo);
        rabbitmqVo.setPayType(payType);
        rabbitmqVo.setTrxId(ShareCodeUtil.generateShortUuid());
        MqSendLog.logData(gson.toJson(rabbitmqVo));
        template.convertAndSend("rewardQueue",rabbitmqVo);
    }

    public void setDiligenceUpdate(User user){
        if (null != user.getId()){
            template.convertAndSend("diligenceUpdate",user.getId());
        }
        if (null != user.getParentFirst()){
            template.convertAndSend("diligenceUpdate",user.getParentFirst());
        }
        if (null != user.getParentSecond()){
            template.convertAndSend("diligenceUpdate",user.getParentSecond());
        }
    }


    /**
     * 发送转赠延时消费
     * @param msg
     * @param times 毫秒
     */
    public void sendDelayTransfer(String msg,long times){
        Message message =buildMessage(msg);
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        message.getMessageProperties().setExpiration(times+"");
        MqSendLog.logData(gson.toJson(msg));
        template.convertAndSend("enqueQueue", message);
    }

    /**
     * 发送发旅游卡
     * @param orderSn
     */
    public void sendTripCard(String orderSn){
        MqSendLog.logData(gson.toJson(orderSn));
        template.convertAndSend("sendTripCard",orderSn);
    }

}
